package com.example.travelservlet.userLabel.controller;

import cn.hutool.log.Log;
import com.example.travelservlet.entity.Label;
import com.example.travelservlet.entity.UserLabel;
import com.example.travelservlet.user.domain.Result;
import com.example.travelservlet.userLabel.service.UserLabelService;
import com.example.travelservlet.utils.ResultUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author 30697
 */
@RestController
@RequestMapping("/userLabel")
public class UserLabelController {
    private final OkHttpClient okHttpClient = new OkHttpClient();

    @Autowired
    private UserLabelService userLabelService;

    @PostMapping("/addUserLabelData")
    private Result addUserLabelData(@Valid String userLabelStr) {
        try {
            String str = URLDecoder.decode(userLabelStr, "utf-8");
            if (str==null||str.isEmpty()){
                return ResultUtil.success(null);
            }else {
                Gson gson = new Gson();
                List<UserLabel> userLabels = gson.fromJson(str,new TypeToken<List<UserLabel>>(){}.getType());
                List<UserLabel> save = userLabelService.createUserLabels(userLabels);
                return ResultUtil.success(save);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

}
